﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CTDownloaer.Core;
using System.Text.RegularExpressions;

namespace CTDownloaer
{
    public partial class Form1 : Form
    {
        private WebSession web;
        private bool _isLogin = false;
        //页面的编码
        private string _charset = "utf-8";
        private readonly string _prefix = "http://page12.ctfile.com";
        private StringBuilder _fileList;
        private int _fileCount;
        private string _basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        public Form1()
        {
            _fileList = new StringBuilder();
            //初始化HTTP会话实例
            web = new WebSession();
            //初始化控件
            InitializeComponent();
            grpLogin.Visible = false;

            //请求一次登录页，拿到Cookie
            var ret = web.Get("http://www.ctfile.com/index.php?item=account&action=login", _charset);
            if (ret.IsError)
            {
                txtLog.AppendText("请求出现错误！");
                return;
            }
            //拿到cookie后，再获取验证码，否则验证码无效
            RefreshCode();
        }
        
        private void btnLogin_Click(object sender, EventArgs e)
        {
            var url = "http://www.ctfile.com/index.php";
            //组织登录请求数据，请打开Chrome
            //点击登录一次，从Network的Request数据中提取
            var loginStr = string.Format("item=account&action=login&task=login&ref=http://home.ctfile.com/&username={0}&password={1}&randcodeV2={2}",txtUser.Text,txtPass.Text,txtCode.Text);
            var ret = web.Post(url, loginStr, _charset);
            //本例中的网盘登录成功后会返回两个字符 ok，否则是不成功，可以自己尝试
            if (ret.Source == "ok") {
                //标记为已登录
                _isLogin = true;
                //隐藏登录
                grpLogin.Visible = false;
            }
        }
        private void RefreshCode() {
            //获取验证码图片的流数据
            var img = web.GetRemoteStream("http://www.ctfile.com/randcodeV2_login.php");
            //显示在picturebox中
            picCheckCode.Image = new Bitmap(img);
        }

        private void picCheckCode_Click(object sender, EventArgs e)
        {
            //刷新验证码
            RefreshCode();
        }

        private void toolLogin_Click(object sender, EventArgs e)
        {
            //显示登录
            grpLogin.Visible = true;
            //重设登录窗的位置
            grpLogin.Location = new Point(256, 54);
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            var url = txtRootUrl.Text;
            if (string.IsNullOrEmpty(url)) return;
            GetList(url);

        }

        private void GetList(string url)
        {



        }

    }
    /// <summary>
    /// 字符串扩展
    /// </summary>
    public static class StringExtensions
    {

        #region 数字
        /// <summary>
        /// 转换为Int32
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int ToInt32(this string num)
        {
            int i;
            return (Int32.TryParse(num, out i)) ? i : 0;
        }
        /// <summary>
        /// 转换为双精度
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static double ToDouble(this string num)
        {
            double i;
            return (Double.TryParse(num, out i)) ? i : 0.0;
        }
        #endregion

        #region 正则相关
        public static string ReplaceEx(this string source, string partner, string replace)
        {
            var r = new Regex(partner);
            return r.Replace(source, replace);
        }
        /// <summary>
        /// 根据正则查找字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="partner">正则表达式</param>
        /// <param name="include">是否含非表达式部分，默认不含</param>
        /// <returns></returns>
        public static string FindEx(this string source, string partner,bool include = false)
        {
            var r = new Regex(partner);
            var ret = "";
            if (!r.IsMatch(source)) return ret;
            var m = r.Match(source);
            ret = include ? m.Value : r.Replace(m.Value, "$1");
            return  ret;
        }

        public static IList<string> FindMoreEx(this string source, string partner, bool include = false)
        {
            var ret = new List<string>();
            var r = new Regex(partner);
            var matches = r.Matches(source);
            if (matches.Count <= 0) return ret;

            foreach (Match m in matches)
            {
                ret.Add(include ? m.Value : r.Replace(m.Value, "$1"));
            }

            return ret;
        }
        /// <summary>
        /// 将source转换为以sp分隔的数组
        /// </summary>
        /// <param name="source"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        public static string[] SplitEx(this string source, string sp)
        {
            return Regex.Split(source, sp, RegexOptions.IgnoreCase);
        }
        public static bool IsMatch(this string source, string partner)
        {
            var r = new Regex(partner);
            return r.IsMatch(source);
        }
        #endregion
    }
}
